// gulp的压缩流程
const fs = require('fs')
const { Transform } = require('stream')

exports.default =  () => {
    // 文件的读取流
    const readStream = fs.createReadStream('src/normalize.css')
    // 文件的写入流
    const writeStream = fs.createWriteStream('src/normalize.min.css')
    // 文件转换流
    const transform = new Transform({
        transform: (chunk, encoding, callback) => {
            // 核心转换过程实现
            // chunk => 读取流中取到的内容(Buffer) 
            const input = chunk.toString()
            const output = input.replace(/\s+/g, '').replace(/\/\*.+?\*\//g, '')
            callback(null, output)  // null是错误处理
        }
    })


    // 把读取出来的文件流导入写入文件流
    readStream
        .pipe(transform)  // 转换
        .pipe(writeStream)
    
    return readStream
}